package org.jsola.hr.web;

import com.alibaba.fastjson.JSON;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.AttendPeopleMonthlyStatAddDTO;
import org.jsola.hr.dto.AttendPeopleMonthlyStatUpdateDTO;
import org.jsola.hr.dto.PunchStatusUpdateDTO;
import org.jsola.hr.query.AttendDailyPeriodQuery;
import org.jsola.hr.query.AttendPeopleDailyQuery;
import org.jsola.hr.query.AttendPeopleMonthlyStatQuery;
import org.jsola.hr.service.IAttendPeopleMonthlyStatService;
import org.jsola.hr.vo.AttendDailyPeriodVO;
import org.jsola.hr.vo.AttendPeopleDailyListVO;
import org.jsola.hr.vo.AttendPeopleMonthlyStatListVO;
import org.jsola.hr.vo.AttendPeopleMonthlyStatVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrAttendPeopleMonthlyStatController")
@RequestMapping(HrConstants.API_PATH + "/v1/attendPeopleMonthlyStats")
public class AttendPeopleMonthlyStatController implements IAttendPeopleMonthlyStatControllerAPI {

    @Autowired
    private IAttendPeopleMonthlyStatService attendPeopleMonthlyStatService;

    @Override
    @PostMapping
    public Result<AttendPeopleMonthlyStatVO> save(@Valid @RequestBody AttendPeopleMonthlyStatAddDTO attendPeopleMonthlyStatAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增打卡人员月统计权限校验

        return Result.success(
                attendPeopleMonthlyStatService.save(attendPeopleMonthlyStatAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody AttendPeopleMonthlyStatUpdateDTO attendPeopleMonthlyStatUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改打卡人员月统计权限校验

        int count = attendPeopleMonthlyStatService.update(attendPeopleMonthlyStatUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{attendPeopleMonthlyStatId}")
    public Result<Integer> delete(@PathVariable String attendPeopleMonthlyStatId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除打卡人员月统计权限校验

        int count = attendPeopleMonthlyStatService.deleteByIds(tokenUser, attendPeopleMonthlyStatId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] attendPeopleMonthlyStatIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除打卡人员月统计权限校验

        int count = attendPeopleMonthlyStatService.deleteByIds(tokenUser, attendPeopleMonthlyStatIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{attendPeopleMonthlyStatId}")
    public Result<AttendPeopleMonthlyStatVO> findById(@PathVariable String attendPeopleMonthlyStatId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看打卡人员月统计权限校验

        return Result.success(
            attendPeopleMonthlyStatService.selectById(attendPeopleMonthlyStatId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<AttendPeopleMonthlyStatListVO>> listAll(@Valid AttendPeopleMonthlyStatQuery attendPeopleMonthlyStatQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看打卡人员月统计权限校验

        return Result.success(
            attendPeopleMonthlyStatService.select(attendPeopleMonthlyStatQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<AttendPeopleMonthlyStatListVO>> page(@Valid AttendPeopleMonthlyStatQuery attendPeopleMonthlyStatQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            attendPeopleMonthlyStatService.selectPage(attendPeopleMonthlyStatQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "monthlyDetail")
    public Result<List<AttendPeopleDailyListVO>> monthlyDetail(AttendPeopleDailyQuery attendPeopleDailyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendPeopleMonthlyStatService.monthlyDetail(attendPeopleDailyQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PutMapping(value = "updatePunchStatus")
    public Result<Integer> updatePunchStatus(@Valid @RequestBody PunchStatusUpdateDTO punchStatusUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendPeopleMonthlyStatService.updatePunchStatus(punchStatusUpdateDTO, tokenUser)
        );
    }

    /**
     * 打卡详情
     *
     * @param attendDailyPeriodQuery
     * @return
     */
    @Override
    @GetMapping(value = "attendDailyPeriodOfMonth")
    public Result<List<AttendDailyPeriodVO>> attendDailyPeriodOfMonth(AttendDailyPeriodQuery attendDailyPeriodQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                attendPeopleMonthlyStatService.attendDailyPeriodOfMonth(attendDailyPeriodQuery, tokenUser.getSiteId())
        );
    }


}

